package com.hellogood.yaohuo.client.basic;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Color;
import com.hellogood.yaohuo.shared.services.ColorService;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class ColorModifyWindow extends Window {

	private TextItem codeItem = null;
	private TextItem nameItem = null;
	private HiddenItem typeItem = null;
	private Button saveBtn = null;
	private Label msg = null;
	private DynamicForm form = null;
	private String type = null;
	private String superCode = null;

	private ColorController cc = null;

	public ColorModifyWindow(ColorController cc, String type, String superCode, String colorType) {
		this.cc = cc;
		this.type = type;
		this.superCode = superCode;
		initView();
		if (colorType != null) {
			setTitle("为 " + colorType + " 创建颜色");
		} else {
			setTitle("创建色系");
		}
	}

	private void initView() {
		LengthRangeValidator length10Validator = new LengthRangeValidator();
		length10Validator.setMin(1);
		length10Validator.setMax(10);
		length10Validator.setErrorMessage(CommonMessages.MaxLength10);
		if (type.equals("type")) {
			codeItem = new TextItem("code", "色系编码");
		} else if (type.equals("color")) {
			codeItem = new TextItem("code", "颜色编码");
		}
		codeItem.setRequired(true);
		codeItem.setKeyPressFilter("[0-9.]");
		codeItem.setValidators(length10Validator);
		codeItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName() != null && event.getKeyName().equals("Enter")) {
					nameItem.focusInItem();
				}
			}
		});

		if (type.equals("type")) {
			nameItem = new TextItem("name", "色系名称");
		} else if (type.equals("color")) {
			nameItem = new TextItem("name", "颜色名称");
		}
		nameItem.setRequired(true);
		nameItem.setValidators(length10Validator);
		nameItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName() != null && event.getKeyName().equals("Enter")) {
					saveBtn.focus();
				}
			}
		});

		typeItem = new HiddenItem("type");
		if (type.equals("type")) {
			typeItem.setValue("type");
		} else if (type.equals("color")) {
			typeItem.setValue("color");
		}

		form = new DynamicForm();
		form.setSize("300", "80");
		form.setFields(codeItem, nameItem, typeItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("350", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(15);
		addChild(layout);
	}

	public TextItem getTextItem() {
		return codeItem;
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				Object code = codeItem.getValue();
				Object name = nameItem.getValue();
				Object type = typeItem.getValue();

				Color c = new Color();
				if (code != null && name != null && type != null) {
					c.setColorCode(code.toString());
					c.setColorName(name.toString());
					c.setType(type.toString());
					c.setSuperCode(superCode);

					ColorService.Util.getInstance().createColor(c, new AsyncCallback<String>() {

						@Override
						public void onSuccess(String result) {
							if (result.equals("")) {
								cc.initData();
								msg.setContents(CommonMessages.CreateSuccess);
								codeItem.setValue("");
								nameItem.setValue("");
								saveBtn.setTitle(CommonMessages.CreateAgain);
							} else {
								msg.setContents(result);
							}
						}

						@Override
						public void onFailure(Throwable caught) {
							msg.setContents(caught.getMessage());
						}
					});

				} else {
					msg.setContents(CommonMessages.CodeNameInputError);
				}
			}
		}

	}
}
